﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Library.Extensions;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Drawing;

namespace Script
{
	public partial class BaseClass : BaseSystemConsole
	{
		/// <summary>
		/// https://blog.csdn.net/Q672405097/article/details/86639488
		/// </summary>
		private static Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

		public static void SetEnvironmentVariable(string key, string value)
		{
			Environment.SetEnvironmentVariable(key, value);
			config.AppSettings.Settings[key].Value = value;
			config.Save(ConfigurationSaveMode.Modified);
		}

		public static string GetEnvironmentVariable(string key)
		{
			var result = Environment.GetEnvironmentVariable(key);
			if (string.IsNullOrEmpty(result)) return null;
			return result;
		}

		static BaseClass()
		{
			foreach (var item in ConfigurationManager.AppSettings.AllKeys)
			{
				Environment.SetEnvironmentVariable(item, config.AppSettings.Settings[item].Value);
			}
		}

		public class StreamWriter : System.IO.StreamWriter
		{
			public StreamWriter(string path) : this(new FileInfo(path)) { }
			public StreamWriter(FileInfo path) : base(path.FullName, false, Encoding.GetEncoding("gbk")) { path.Directory.Create(); }
		}
	}

	public partial class BaseClass
	{
		public void WriteAllLines(Dictionary<string, string> dic, string path)
		{
			using (var streamWriter = new StreamWriter(Path.ChangeExtension(InputPath, ".csv")))
			{
				streamWriter.WriteLine("key,value");
				foreach (var item in dic)
				{
					streamWriter.WriteLine($"{item.Key},{item.Value}");
				}
			}
		}

		public void CreateDirectory(string name)
		{
			var path = GetType().Name + "/" + name;
			new DirectoryInfo(path).Create();
		}

		public IEnumerable GetExcelCell(string path)
		{
			using (Image image = new Bitmap(path, false))
			{
				int width = image.Width;
				int height = image.Height;


				int max = Math.Max(width, height);
				if (max == width)
				{
					if (max > 350)
					{
						height = 350 * height / width;
						width = 350;
					}
				}
				else
				{
					if (max > 350)
					{
						width = 350 * width / height;
						height = 350;
					}
				}
				yield return string.Format("<table><img src='{0}' width='{1}' height='{2}'>", path, width, height);
				yield return image;
			}

			GC.Collect();
		}

		public Image GetExcelImage(string path)
		{
            try
            {
				Image image = new Bitmap(path, false);
				{
					int width = image.Width;
					int height = image.Height;


					int max = Math.Max(width, height);
					if (max == width)
					{
						if (max > 350)
						{
							height = 350 * height / width;
							width = 350;
						}
					}
					else
					{
						if (max > 350)
						{
							width = 350 * width / height;
							height = 350;
						}
					}
					return image;
				}
			}
            catch (Exception)
            {
				return null;
            }
			finally
            {
				GC.Collect();
			}
		}

		protected IEnumerable<string> GetPaths(string extension = ".txt")
		{
			extension = Path.GetExtension(extension);

			var path = SystemConsole.GetInputStr("请拖入选定（文件夹或文件）:");

			if (Directory.Exists(path))
			{
				var newPath = Path.ChangeExtension(path, extension);
				if (File.Exists(newPath))
					yield return newPath;
				else
					foreach (var item in Directory.GetFiles(path, "*" + extension, SearchOption.AllDirectories))
						yield return item;

			}
			else
			{
				if (File.Exists(path) && Path.GetExtension(path) == extension)
					yield return path;
			}
		}
	}
}